package com.falcon.authority.api;

import cn.hutool.core.bean.BeanUtil;
import com.falcon.authority.domain.dos.UserDTO;
import com.falcon.authority.domain.po.ApiPO;
import com.falcon.authority.domain.po.RolePO;
import com.falcon.authority.domain.po.UserPO;
import com.falcon.authority.mapper.IApiMapper;
import com.falcon.authority.mapper.IUserMapper;
import com.falcon.authority.service.IRoleService;
import com.swan.mybatis.condition.Condition;
import com.swan.mybatis.condition.OpType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author zongf
 * @date 2025-02-07
 **/
@Slf4j
@Service
public class UserApi implements IUserApi {

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    private IApiMapper apiMapper;

    @Autowired
    private IRoleService roleService;

    @Override
    public UserDTO queryUserByUsername(String username) {

        Condition condition = Condition.newInstance()
                .and(UserPO.Fields.userName, OpType.equals, username);

        UserPO userDO = this.userMapper.select(condition);
        return Objects.nonNull(userDO) ? BeanUtil.copyProperties(userDO, UserDTO.class) : null;
    }

    @Override
    public List<RolePO> queryUserRoles(Long id) {
        return this.roleService.queryRoleListByUserId(id);
    }

    @Override
    public List<ApiPO> queryUserApis(Long userId) {
        log.info("查询用户权限, userId:{}", userId);
        return this.apiMapper.selectAuthApis(userId);
    }
}
